package com.wwwarehouse.usercenter.fragment.chain_triangles;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskTaskResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.share.ShareContent;
import com.wwwarehouse.common.custom_widget.share.ShareItem;
import com.wwwarehouse.common.custom_widget.share.SharePopuwindow;
import com.wwwarehouse.common.eventbus_event.TCEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.SharedPreferencesHelper;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.chain_triangles.ManagerUserInfoBean;
import com.wwwarehouse.usercenter.bean.chain_triangles.SendInviteBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static android.content.ContentValues.TAG;

/**
 * Created by xiaoke.ni on 2017/8/28.
 */

@Route(path = UserCenterConstant.PATH_TC_FRAGMWNT)
public class TriangulationChainFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private RelativeLayout mCardRl;
    private LinearLayout mTcFrontll;//正面所有元素
    private Button mLookTcExplainBtn;//演示（翻面）
    private ImageView mTcFlipIv;//翻面按钮
    private LinearLayout mTcTitleLl;//双行标题
    private TextView mTcTitle1Tv;//双行标题1
    private TextView mTcTitle2Tv;//双行标题2
    private TextView mTcTitleSingleLineTv;//单行标题
    private CircleImageView mTcCreatorIv;//创建者
    private TextView mTcAdministratorTv;//管理者
    private CircleImageView mTcAdministratorIv;//管理者
    private TextView mOrTv;
    private TextView mTcAdministratorSelfTv;//我（管理者）
    private Button mTcAdministratorRevocationBtn;//撤销权限（管理者）
    private TextView mTcSupervisorTv;//监控者
    private CircleImageView mTcSupervisorIv;//监控者
    private TextView mOr2Tv;
    private TextView mTcSupervisorSelfTv;//我（监控者）
    private Button mTcSupervisorRevocationBtn;//撤销权限（监控者）

    private SharePopuwindow popuwindow;

    public static boolean hasAT, hasSV;
    private SharedPreferencesHelper sp;

    final int wechat_tag = 1001, f2f_tag = 1002, msg_tag = 1003, comit_tag = 1004;
    int requestCount = 2;
    final int CODE_ADMINISTRATOR = 20, CODE_SUPERVISOR = 21;
    String buId;
    private String cardUkid;
    int authType;//权限类型 10：普通邀请 20：一级管理者邀请 21：一级监控者邀请 30：带权限邀请
    int inviteType;
    ManagerUserInfoBean managerUserInfoBean;
    private int from;//0建立1管理

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_triangulation_chain, null);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
    }

    private void initView() {
        mCardRl = (RelativeLayout) mView.findViewById(R.id.card_rl);
        mTcFrontll = (LinearLayout) mView.findViewById(R.id.tc_front_ll);
        mLookTcExplainBtn = (Button) mView.findViewById(R.id.look_tc_explain_btn);
        mTcFlipIv = (ImageView) mView.findViewById(R.id.tc_flip_iv);
        mTcTitleLl = (LinearLayout) mView.findViewById(R.id.tc_title_ll);
        mTcTitle1Tv = (TextView) mView.findViewById(R.id.tc_title_1_tv);
        mTcTitle2Tv = (TextView) mView.findViewById(R.id.tc_title_2_tv);
        mTcTitleSingleLineTv = (TextView) mView.findViewById(R.id.tc_title_singleLine_tv);
        mTcCreatorIv = (CircleImageView) mView.findViewById(R.id.tc_creator_iv);
        mTcAdministratorTv = (TextView) mView.findViewById(R.id.tc_administrator_tv);
        mTcAdministratorIv = (CircleImageView) mView.findViewById(R.id.tc_administrator_iv);
        mOrTv = (TextView) mView.findViewById(R.id.or_tv);
        mTcAdministratorSelfTv = (TextView) mView.findViewById(R.id.tc_administrator_self_tv);
        mTcAdministratorRevocationBtn = (Button) mView.findViewById(R.id.tc_administrator_revocation_btn);
        mTcSupervisorTv = (TextView) mView.findViewById(R.id.tc_supervisor_tv);
        mOr2Tv = (TextView) mView.findViewById(R.id.or2_tv);
        mTcSupervisorIv = (CircleImageView) mView.findViewById(R.id.tc_supervisor_iv);
        mTcSupervisorSelfTv = (TextView) mView.findViewById(R.id.tc_supervisor_self_tv);
        mTcSupervisorRevocationBtn = (Button) mView.findViewById(R.id.tc_supervisor_revocation_btn);
    }

    private void initEvent() {
        mLookTcExplainBtn.setOnClickListener(this);
        mTcFlipIv.setOnClickListener(this);
        mTcAdministratorTv.setOnClickListener(this);
        mTcAdministratorSelfTv.setOnClickListener(this);
        mTcAdministratorRevocationBtn.setOnClickListener(this);
        mTcSupervisorTv.setOnClickListener(this);
        mTcSupervisorSelfTv.setOnClickListener(this);
        mTcSupervisorRevocationBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tc_flip_iv) {
//            if (mTcFrontll.getVisibility() == View.VISIBLE) {
//                mTcFrontll.setVisibility(View.GONE);
//                mLookTcExplainBtn.setVisibility(View.VISIBLE);
//            } else {
//                mTcFrontll.setVisibility(View.VISIBLE);
//                mLookTcExplainBtn.setVisibility(View.GONE);
//            }

            com.wwwarehouse.common.tools.AnimationUtils.cardTransAnims(mCardRl, mTcFrontll, mLookTcExplainBtn, null);
        } else if (id == R.id.tc_administrator_tv) {
            authType = CODE_ADMINISTRATOR;
            showSelectWindow();
        } else if (id == R.id.tc_administrator_self_tv) {
            DialogTools.getInstance().showTCDialog(getActivity(), "", getResources().getString(R.string.sure_me_managers), getResources().getString(R.string.not_go_finger), getResources().getString(R.string.confirm_specified),
                    new DialogTools.ConfirmListener() {
                        @Override
                        public void setConfirmListener(Dialog dialog, View view, String s) {
                            dialog.dismiss();
                        }
                    }, new DialogTools.DismissListener() {
                        @Override
                        public void setDismissListener(Dialog dialog, View view) {
                            assignManager("regulator");
                            dialog.dismiss();
                        }
                    });
        } else if (id == R.id.tc_administrator_revocation_btn) {
            DialogTools.getInstance().showTCDialog(getActivity(), "", getResources().getString(R.string.sure_undo_managers), getResources().getString(R.string.not_go_finger), getResources().getString(R.string.undo),
                    new DialogTools.ConfirmListener() {
                        @Override
                        public void setConfirmListener(Dialog dialog, View view, String s) {
                            dialog.dismiss();
                        }
                    }, new DialogTools.DismissListener() {
                        @Override
                        public void setDismissListener(Dialog dialog, View view) {

                            revocationAccredit("regulator");
                            dialog.dismiss();
                        }
                    });
        } else if (id == R.id.tc_supervisor_tv) {
            authType = CODE_SUPERVISOR;
            showSelectWindow();
        } else if (id == R.id.tc_supervisor_self_tv) {
            DialogTools.getInstance().showTCDialog(getActivity(), "", getResources().getString(R.string.sure_me_monitoring), getResources().getString(R.string.not_go_finger), getResources().getString(R.string.confirm_specified),
                    new DialogTools.ConfirmListener() {
                        @Override
                        public void setConfirmListener(Dialog dialog, View view, String s) {
                            dialog.dismiss();
                        }
                    }, new DialogTools.DismissListener() {
                        @Override
                        public void setDismissListener(Dialog dialog, View view) {
                            assignManager("supervisor");
                            dialog.dismiss();
                        }
                    });
        } else if (id == R.id.tc_supervisor_revocation_btn) {
            DialogTools.getInstance().showTCDialog(getActivity(), "", getResources().getString(R.string.sure_undo_monitoring), getResources().getString(R.string.not_go_finger), getResources().getString(R.string.undo),
                    new DialogTools.ConfirmListener() {
                        @Override
                        public void setConfirmListener(Dialog dialog, View view, String s) {
                            dialog.dismiss();
                        }
                    }, new DialogTools.DismissListener() {
                        @Override
                        public void setDismissListener(Dialog dialog, View view) {
                            revocationAccredit("supervisor");
                            dialog.dismiss();
                        }
                    });
        } else if (id == R.id.look_tc_explain_btn) {
            showDialog();
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof TriangulationChainFragment) {
            if (getArguments() != null) {
                from = getArguments().getInt("from", 0);
            }
//        if (from == 0)
//            mActivity.setTitle("建立三角链");
//        else
            mActivity.setTitle(getResources().getString(R.string.set_triangular_management));
        }
    }

    @Override
    public void requestDatas() {
        mTcAdministratorIv.setImageResource(R.drawable.tc_image_background);
        mTcCreatorIv.setImageResource(R.drawable.tc_image_background);
        mTcSupervisorIv.setImageResource(R.drawable.tc_image_background);
        mTcSupervisorSelfTv.setText(getResources().getString(R.string.self));
        mTcSupervisorSelfTv.setClickable(true);
        mTcSupervisorSelfTv.getPaint().setUnderlineText(true);
        mTcAdministratorSelfTv.setText(getResources().getString(R.string.self));
        mTcAdministratorSelfTv.setClickable(true);
        mTcAdministratorSelfTv.getPaint().setUnderlineText(true);
        EventBus.getDefault().register(this);
        mTcAdministratorSelfTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mTcSupervisorSelfTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        CardDeskTaskResponseBean.TaskBean taskBean = (CardDeskTaskResponseBean.TaskBean) getArguments().getSerializable(Constant.KEY_CARD_DESK_TASK_DETAILS);
        if (from == 0)
            cardUkid = taskBean.getCardUkid();
        else
            buId = getArguments().getString("buId");
        sp = new SharedPreferencesHelper(getActivity());
        if (!sp.getBooleanValue("isTcShowed", false) && from == 0) {
            showDialog();
        }
        getManagerUserInfo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }


    int i = 1;

    private void showDialog() {
//        AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(getActivity(), R.anim.alpha_start);
        final Dialog dialog = new Dialog(mActivity, R.style.Transparent);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.tc_dialog);
        dialog.show();
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        final View viewDialog = inflater.inflate(R.layout.dialog_tc_boot, null);
        final View view = viewDialog.findViewById(R.id.tc_boot_v);
        Display display = mActivity.getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        //设置dialog的宽高为屏幕的宽高
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height);
        dialog.setContentView(viewDialog, layoutParams);
        viewDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i++;
                switch (i) {
                    case 2:
                        view.setBackground(getResources().getDrawable(R.drawable.tc_boot_2));
                        break;
                    case 3:
                        view.setBackground(getResources().getDrawable(R.drawable.tc_boot_3));
                        break;
                    case 4:
                        view.setBackground(getResources().getDrawable(R.drawable.tc_boot_4));
                        break;
                    case 5:
                        view.setBackground(getResources().getDrawable(R.drawable.tc_boot_5));
                        break;
                    case 6:
                        view.setBackground(getResources().getDrawable(R.drawable.tc_boot_6));
                        break;
                    case 7:
                        i = 1;
                        dialog.dismiss();
                        sp.putBooleanValue("isTcShowed", true);
                        mTcFrontll.setVisibility(View.VISIBLE);
                        mLookTcExplainBtn.setVisibility(View.GONE);
                        break;
                }

            }
        });
    }

    /**
     * 三角链-查询接口，查询三个角色的信息
     */
    private void getManagerUserInfo() {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        if (from == 0)
            hashMap.put("cardUkid", cardUkid);//临时任务需传
        else
            hashMap.put("buId", buId);//功能卡片进入时要传 10001350
        NoHttpUtils.httpPost(UserCenterConstant.GET_MANAGER_USER_INFO, hashMap, onResponseListener, 0);
        showProgressDialog();

    }

    /**
     * 三角链-撤销授权
     */
    private void revocationAccredit(String manageType) {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("buId", buId);//业务单位ID @mock=555
        hashMap.put("cardType", from == 0 ? "task" : "function");//卡片类型
        hashMap.put("manageType", manageType);//管理类型
        NoHttpUtils.httpPost(UserCenterConstant.REVOCATION_ACCREDIT, hashMap, onResponseListener, 1);
        showProgressDialog();

    }

    /**
     * 三角链-查看站内通讯录是否有人
     */
    private void checkAddressList() {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        NoHttpUtils.httpPost(UserCenterConstant.CHECK_ADDRESS_LIST, hashMap, onResponseListener, 2);

    }

    /**
     * 三角链-发送邀请链接
     */
    private void sendInvite() {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("authType", authType);//权限类型 10：普通邀请 20：一级管理者邀请 21：一级监控者邀请 30：带权限邀请
        hashMap.put("buId", buId);
        hashMap.put("inviteType", inviteType);//邀请类型 邀请类型(1：微信邀请 2：短信邀请 3：面对面邀请)
        NoHttpUtils.httpPost(UserCenterConstant.SEND_INVITE, hashMap, onResponseListener, 3);
        showProgressDialog();
    }

    /**
     * 三角链-指定一级管理者/监控者
     */
    private void assignManager(String manageType) {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("assignType", "oneself");//APP 网仓APP扫一扫 mobile 站内通讯录指定 oneself 指定给自己
        hashMap.put("buId", buId);//assignType=mobile,oneself必填
        hashMap.put("manageType", manageType);//管理类型 assignType=mobile,oneself必填 （supervisor一级监控者 regulator一级管理者）
        NoHttpUtils.httpPost(UserCenterConstant.ASSIGN_MANAGER, hashMap, onResponseListener, 4);
        showProgressDialog();
    }


    NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        managerUserInfoBean = JSON.parseObject(commonClass.getData().toString(), ManagerUserInfoBean.class);
                        String title = managerUserInfoBean.getBuName().toString().trim();
                        if (from == 0)
                            buId = Integer.toString(managerUserInfoBean.getBuId());
                        if (title.length() <= 10) {
                            mTcTitleLl.setVisibility(View.GONE);
                            mTcTitleSingleLineTv.setVisibility(View.VISIBLE);
                            mTcTitleSingleLineTv.setText("『" + title + "』");
                        } else {
                            mTcTitleLl.setVisibility(View.VISIBLE);
                            mTcTitleSingleLineTv.setVisibility(View.GONE);
                            mTcTitle1Tv.setText(title.substring(0, 8));
                            mTcTitle2Tv.setText(title.substring(8));
                        }

                        if (managerUserInfoBean.getCreator() != null && managerUserInfoBean.getCreator().getFaceUrl() != null)
                            url2Drawable(managerUserInfoBean.getCreator().getFaceUrl().toString(), mTcCreatorIv);
                        //管理者
                        if (managerUserInfoBean.getRegulator() != null && (managerUserInfoBean.getRegulator().getUserId() != null || managerUserInfoBean.getRegulator().getInviteUkid() != null)) {
                            hasAT = true;
                            mOrTv.setVisibility(View.GONE);
                            mTcAdministratorSelfTv.getPaint().setUnderlineText(false);
                            mTcAdministratorSelfTv.setClickable(false);
                            mTcAdministratorRevocationBtn.setVisibility(View.VISIBLE);
                            mTcAdministratorTv.setClickable(false);
                            if (managerUserInfoBean.getRegulator().getName() != null && !managerUserInfoBean.getRegulator().getName().toString().isEmpty()) {
                                mTcAdministratorSelfTv.setText(managerUserInfoBean.getRegulator().getName().toString());
                                mTcAdministratorSelfTv.setVisibility(View.VISIBLE);
                            } else {
                                mTcAdministratorSelfTv.setText("");
                                mTcAdministratorSelfTv.setVisibility(View.GONE);
                            }
                            if (managerUserInfoBean.getRegulator().getStatus() != null && !managerUserInfoBean.getRegulator().getStatus().toString().isEmpty()) {
                                mTcAdministratorTv.setText(managerUserInfoBean.getRegulator().getStatus().toString());
                            } else {
                                mTcAdministratorTv.setText("");
                            }
                            if (managerUserInfoBean.getRegulator() != null && managerUserInfoBean.getRegulator().getFaceUrl() != null)
                                url2Drawable(managerUserInfoBean.getRegulator().getFaceUrl().toString(), mTcAdministratorIv);
                        } else {
                            hasAT = false;
                            mOrTv.setVisibility(View.VISIBLE);
                            mTcAdministratorRevocationBtn.setVisibility(View.INVISIBLE);
                            mTcAdministratorTv.setClickable(true);
                            mTcAdministratorSelfTv.getPaint().setUnderlineText(true);
                            mTcAdministratorSelfTv.setClickable(true);
                        }

                        //监控者
                        if (managerUserInfoBean.getSupervisor() != null && (managerUserInfoBean.getSupervisor().getUserId() != null || managerUserInfoBean.getSupervisor().getInviteUkid() != null)) {
                            hasSV = true;
                            mOr2Tv.setVisibility(View.GONE);
                            mTcSupervisorSelfTv.getPaint().setUnderlineText(false);
                            mTcSupervisorSelfTv.setClickable(false);
                            mTcSupervisorRevocationBtn.setVisibility(View.VISIBLE);
                            mTcSupervisorTv.setClickable(false);
                            if (managerUserInfoBean.getSupervisor().getName() != null && !managerUserInfoBean.getSupervisor().getName().toString().isEmpty()) {
                                mTcSupervisorSelfTv.setText(managerUserInfoBean.getSupervisor().getName());
                                mTcSupervisorSelfTv.setVisibility(View.VISIBLE);
                            } else {
                                mTcSupervisorSelfTv.setText("");
                                mTcSupervisorSelfTv.setVisibility(View.GONE);
                            }
                            if (managerUserInfoBean.getSupervisor().getStatus() != null && !managerUserInfoBean.getSupervisor().getStatus().toString().isEmpty()) {
                                mTcSupervisorTv.setText(managerUserInfoBean.getSupervisor().getStatus().toString());
                            } else {
                                mTcSupervisorTv.setText("");
                            }
                            if (managerUserInfoBean.getSupervisor() != null && managerUserInfoBean.getSupervisor().getFaceUrl() != null)
                                url2Drawable(managerUserInfoBean.getSupervisor().getFaceUrl(), mTcSupervisorIv);
                        } else {
                            hasSV = false;
                            mOr2Tv.setVisibility(View.VISIBLE);
                            mTcSupervisorRevocationBtn.setVisibility(View.INVISIBLE);
                            mTcSupervisorTv.setClickable(true);
                            mTcSupervisorSelfTv.getPaint().setUnderlineText(true);
                            mTcSupervisorSelfTv.setClickable(true);
                        }

                    } else {
                        mTcAdministratorTv.setClickable(false);
                        mTcSupervisorTv.setClickable(false);
                        mTcAdministratorSelfTv.setClickable(false);
                        mTcSupervisorSelfTv.setClickable(false);
                        mTcAdministratorRevocationBtn.setVisibility(View.INVISIBLE);
                        mTcSupervisorRevocationBtn.setVisibility(View.INVISIBLE);
                    }
                    break;
                case 1:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        org.json.JSONObject json = null;
                        try {
                            json = new org.json.JSONObject(commonClass.getData().toString());
                            if ("regulator".equals(json.getString("manageType"))) {
                                hasAT = false;
                                mTcAdministratorTv.setText(getResources().getString(R.string.click_choice));
                                mTcAdministratorIv.setImageDrawable(getResources().getDrawable(R.drawable.tc_image_background));
                                mOrTv.setVisibility(View.VISIBLE);
                                mTcAdministratorSelfTv.setText(getResources().getString(R.string.self));
                                mTcAdministratorSelfTv.getPaint().setUnderlineText(true);
                                mTcAdministratorSelfTv.setVisibility(View.VISIBLE);
                                mTcAdministratorSelfTv.setClickable(true);
                                mTcAdministratorRevocationBtn.setVisibility(View.INVISIBLE);
                                mTcAdministratorTv.setClickable(true);
                            } else if ("supervisor".equals(json.getString("manageType"))) {
                                hasSV = false;
                                mTcSupervisorTv.setText(getResources().getString(R.string.click_choice));
                                mTcSupervisorIv.setImageDrawable(getResources().getDrawable(R.drawable.tc_image_background));
                                mOr2Tv.setVisibility(View.VISIBLE);
                                mTcSupervisorSelfTv.setText(getResources().getString(R.string.self));
                                mTcSupervisorSelfTv.setVisibility(View.VISIBLE);
                                mTcSupervisorSelfTv.getPaint().setUnderlineText(true);
                                mTcSupervisorSelfTv.setClickable(true);
                                mTcSupervisorRevocationBtn.setVisibility(View.INVISIBLE);
                                mTcSupervisorTv.setClickable(true);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } else {

                    }
                    break;
                case 2:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        popuwindow.changeShowType(comit_tag, ShareItem.TypeState.CONTENT);
                    } else if (TextUtils.equals("70019", commonClass.getCode())) {
                        popuwindow.changeShowType(comit_tag, ShareItem.TypeState.NONE);
                    }
                    break;
                case 3:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        SendInviteBean sendInviteBean = JSONObject.parseObject(commonClass.getData().toString(), SendInviteBean.class);
                        if (inviteType == 1) {
                            String name = authType == CODE_ADMINISTRATOR ? getResources().getString(R.string.administrator) : getResources().getString(R.string.supervisor);
                            Common.getInstance().shareWeixinFriend(getActivity(), getResources().getString(R.string.invitation_appointed) + name, managerUserInfoBean.getBuName() + getResources().getString(R.string.the) + managerUserInfoBean.getCreator().getName() + getResources().getString(R.string.will) + name + getResources().getString(R.string.give_you), sendInviteBean.getImageUrl(), sendInviteBean.getInviteUrl());
                        } else if (inviteType == 2) {
                            sendSMS("", sendInviteBean.getSmsContent());
                        } else if (inviteType == 3) {
                            TCF2FFragment tcf2FFragment = new TCF2FFragment();
                            Bundle bundle = new Bundle();
                            bundle.putString("inviteCode", sendInviteBean.getInviteCode());
                            bundle.putString("inviteUrl", sendInviteBean.getInviteUrl());
                            bundle.putInt("authType", authType);
                            bundle.putInt("from", from);//0建立1管理
                            tcf2FFragment.setArguments(bundle);
                            pushFragment(tcf2FFragment);
                        }
                    }
                    break;
                case 4:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        getManagerUserInfo();
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            Log.d(TAG, result);
            requestCount++;
            if (what == 2) {
                if (requestCount < 3) {
                    checkAddressList();
                } else {
                    popuwindow.changeShowType(comit_tag, ShareItem.TypeState.NONE);
                }
            }
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

    private void url2Drawable(final String url, final ImageView mImageView) {
        BaseApplication.getApplicationInstance().displayImg(url, mImageView);
    }


    private void showSelectWindow() {
        requestCount = 0;
        checkAddressList();
        final ShareContent content = new ShareContent();
//        content.setContent("我是内容凤飞飞凤飞飞凤飞飞凤飞飞凤飞飞凤飞飞凤飞飞凤飞飞");
//        content.setTitle("我是标题");
//        content.setImageUrl("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=350573396,473598857&fm=26&gp=0.jpg");
//        content.setUrl("http://www.baidu.com");
//        content.setPhoneNum("23234141");
//        content.setMessage("我是短信内容");
//        content.setCopyContent("我是拷贝的内容");
//        content.setShareDestname("目的名字");
        popuwindow = SharePopuwindow.getInstance(getActivity())
                .setContent(content)
                .addItem(com.wwwarehouse.common.R.drawable.share_pop_wechat, getResources().getString(R.string.weCchat_friends), wechat_tag)
                .addItem(com.wwwarehouse.common.R.drawable.share_pop_facetoface, getResources().getString(R.string.f2f), f2f_tag)
                .addItem(com.wwwarehouse.common.R.drawable.share_pop_message, getResources().getString(R.string.msg), msg_tag)
                .addItem(com.wwwarehouse.common.R.drawable.share_pop_commit, getResources().getString(R.string.address_book), comit_tag)
                .setOnSheetItemClickListener(new SharePopuwindow.OnSheetItemClickListener() {
                    @Override
                    public void onClick(SharePopuwindow sharePopuwindow, Object tag) {
                        switch ((int) tag) {
                            case wechat_tag:
                                inviteType = 1;
                                sendInvite();
                                break;
                            case f2f_tag:
                                inviteType = 3;
                                sendInvite();
                                break;
                            case msg_tag:
                                inviteType = 2;
                                sendInvite();
                                break;
                            case comit_tag:
                                TCShowInvitedFragment showInvitedFragment = new TCShowInvitedFragment();
                                Bundle bundle = new Bundle();
                                bundle.putString("buId", buId);
                                bundle.putString("manageType", authType == 20 ? "regulator" : "supervisor");
                                bundle.putInt("from", from);
                                showInvitedFragment.setArguments(bundle);
                                pushFragment(showInvitedFragment);
                                break;
                        }
                        sharePopuwindow.hidePop();
                    }
                })
                .showCancleBtn(true);
        popuwindow.showPop();
        popuwindow.changeShowType(comit_tag, ShareItem.TypeState.LOADING);

    }

    /**
     * 发送短信
     *
     * @param smsto   联系人
     * @param smsBody 短信内容
     */
    private void sendSMS(String smsto, String smsBody) {
//"smsto:xxx" xxx是可以指定联系人的
        Uri smsToUri = Uri.parse("smsto:" + smsto);
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
//"sms_body"必须一样，smsbody是发送短信内容content
        intent.putExtra("sms_body", smsBody);
        startActivity(intent);

    }

    //面对面邀请
//    public void onEventMainThread(AssignManagerEvent event) {
//        String message = event.getResult();
//        AssignManagerBean assignManagerBean = JSON.parseObject(message, AssignManagerBean.class);
//
//        if (authType == CODE_ADMINISTRATOR) {
//            //管理者
//            if (assignManagerBean.getData().getRegulatorBean() == null || assignManagerBean.getData().getRegulatorBean().getUserId() == null) {
//                hasAT = false;
//                mTcAdministratorRevocationBtn.setVisibility(View.GONE);
//            } else {
//                hasAT = true;
//                mTcAdministratorRevocationBtn.setVisibility(View.VISIBLE);
//                mTcAdministratorTv.setText("");
//            }
//            if (assignManagerBean.getData().getRegulatorBean().getName() != null && !assignManagerBean.getData().getRegulatorBean().getName().isEmpty()) {
//                mOrTv.setVisibility(View.GONE);
//                mTcAdministratorSelfTv.setText(assignManagerBean.getData().getRegulatorBean().getName());
//                mTcAdministratorSelfTv.getPaint().setUnderlineText(false);
//                mTcAdministratorSelfTv.setClickable(false);
//            } else {
//
//            }
//            if (assignManagerBean.getData().getRegulatorBean() != null && assignManagerBean.getData().getRegulatorBean().getFaceUrl() != null)
//                url2Drawable(assignManagerBean.getData().getRegulatorBean().getFaceUrl(), mTcAdministratorIv);
//
//        } else if (authType == CODE_SUPERVISOR) {
//            //监控者
//            if (assignManagerBean.getData().getSupervisorBean() == null || assignManagerBean.getData().getSupervisorBean().getUserId() == null) {
//                hasSV = false;
//                mTcSupervisorRevocationBtn.setVisibility(View.GONE);
//            } else {
//                hasSV = true;
//                mTcSupervisorRevocationBtn.setVisibility(View.VISIBLE);
//                mTcSupervisorTv.setText("");
//            }
//            if (assignManagerBean.getData().getSupervisorBean().getName() != null && !assignManagerBean.getData().getSupervisorBean().getName().isEmpty()) {
//                mOr2Tv.setVisibility(View.GONE);
//                mTcSupervisorSelfTv.setText(managerUserInfoBean.getSupervisor().getName());
//                mTcSupervisorSelfTv.getPaint().setUnderlineText(false);
//                mTcSupervisorSelfTv.setClickable(false);
//            }
//            if (assignManagerBean.getData().getSupervisorBean() != null && assignManagerBean.getData().getSupervisorBean().getFaceUrl() != null)
//                url2Drawable(assignManagerBean.getData().getSupervisorBean().getFaceUrl(), mTcSupervisorIv);
//
//
//        }
//
//    }

    //刷新界面
    public void onEventMainThread(TCEvent event) {
        getManagerUserInfo();
    }

    public static boolean hasAT_SV() {
        return hasAT && hasSV;
    }

}
